-
實現背包問題
package problem
1. 問題描述
假設有一個能裝入總體積為T的背包和n件體積分別為w1 , w2 , … , wn 的物品,能否從n件物品中挑選若干件恰好裝滿背包,即使w1 +w2 + … + wn=T,要求找出所有滿足上述條件的解。例如:當T=10,各件物品的體積{1,8,4,3,5,2}時,可找到下列4組解: (1,4,3,2)、(1,4,5)、(8,2)、(3,5,2)。
2. 基本要求
讀入T、n、w1 , w2 , … , wn
3.提示:
可利用遞歸方法:若選中w1 則問題變成在w2 , … , wn 中挑選若干件使得其重量之和為T- w1 ,若不選中w1,則問題變成在w2 , … , wn 中挑選若干件使得其重量之和為T 。依次類推。
也可利用回溯法的設計思想來解決背包問題。首先將物品排成一列,然后順序選取物品裝入背包,假設已選取了前i 件物品之后背包還沒有裝滿,則繼續選取第i+1件物品,若該件物品“太大”不能裝入,則棄之而繼續選取下一件,直至背包裝滿為止。但如果在剩余的物品中找不到合適的物品以填滿背包,則說明“剛剛”裝入背包的那件物品“不合適”,應將它取出“棄之一邊”,繼續再從“它之后”的物品中選取,如此重復,,直至求得滿足條件的解,或者無解。
注:沒壓縮密碼
標簽:
package
problem
體積
w2
上傳時間:
2014-01-18
上傳用戶:yxgi5
-
代入法的啟發示搜索
我的代碼實現是:按照自然語言各字母出現頻率的大小從高到低(已經有人作國統計分析了)先生成一張字母出現頻率統計表(A)--------(e),(t,a,o,i,n,s,h,r),(d,l),(c,u,m,w,f,g,y,p,b),(v,k,j,x,q,z)
,再對密文字母計算頻率,并按頻率從高到低生成一張輸入密文字母的統計表(B),通過兩張表的對應關系,不斷用A中的字母去替換B中的字母,搜索不成功時就回退,在這里回朔是一個關鍵。
標簽:
字母
頻率
搜索
代碼
上傳時間:
2015-10-24
上傳用戶:wanqunsheng
-
最優服務次序問題 問題描述: 設有n 個顧客同時等待一項服務。顧客i需要的服務時間為t(i),i=1,…,n 。...個顧客等待服務時間的 總和除以n。 編程任務: 對于給定的n個顧客需要的服務時間,編程計算最優服務次序。
標簽:
服務
等待
編程
上傳時間:
2013-12-19
上傳用戶:epson850
-
Program to plot x(t) =1/2+∑_(n=1)^∞▒ (sinc 〖n/2〗 〖 cos 〗 〖2πnt/4〗 )
Include n-value (Number of Terms) in graph on plot.
Show 2 graphs, original and simulated together.
標簽:
8289
Program
Include
9618
上傳時間:
2014-01-03
上傳用戶:tb_6877751
-
learningMatlab
PhÇ n 1
c¬ së Mat lab
Ch ¬ ng 1:
Cµ i ® Æ t matlab
1.1.Cµ i ® Æ t ch ¬ ng tr×nh:
Qui tr×nh cµ i ® Æ t Matlab còng t ¬ ng tù nh viÖ c cµ i ® Æ t c¸ c ch ¬ ng tr×nh phÇ n mÒ m kh¸ c, chØ cÇ n theo c¸ c h íng dÉ n vµ bæ xung thª m c¸ c th« ng sè cho phï hî p.
1.1.1 Khë i ® éng windows.
1.1.2 Do ch ¬ ng tr×nh ® î c cÊ u h×nh theo Autorun nª n khi g¾ n dÜ a CD vµ o æ ® Ü a th× ch ¬ ng tr×nh tù ho¹ t ® éng, cö a sæ
標簽:
learningMatlab
172
199
173
上傳時間:
2013-12-20
上傳用戶:lanwei
-
metricmatlab
ch ¬ ng 4
Ma trË n - c¸ c phÐ p to¸ n vÒ ma trË n.
4.1 Kh¸ i niÖ m:
- Trong MATLAB d÷ liÖ u ® Ó ® a vµ o xö lý d íi d¹ ng ma trË n.
- Ma trË n A cã n hµ ng, m cét ® î c gä i lµ ma trË n cì n m. § î c ký hiÖ u An m
- PhÇ n tö aij cñ a ma trË n An m lµ phÇ n tö n» m ë hµ ng thø i, cét j .
- Ma trË n ® ¬ n ( sè ® ¬ n lÎ ) lµ ma trË n 1 hµ ng 1 cét.
- Ma trË n hµ ng ( 1 m ) sè liÖ u ® î c bè trÝ trª n mét hµ ng.
a11 a12 a13 ... a1m
- Ma trË n cét ( n 1) sè liÖ u ® î c bè trÝ trª n 1 cét.
標簽:
metricmatlab
203
184
tr
上傳時間:
2017-07-29
上傳用戶:來茴
-
設信號 ,用 對x(t)采樣得x(n),是否會發生頻譜混疊?現利用FFT分析其頻譜。
1.編程繪制該信號的波形。
2.若令N=16,編程對x(n)做FFT運算,并繪制其幅頻特性曲線。
3.令N=1024,編程對x(n)做FFT運算,并繪制其幅頻特性曲線。
4.分析2、3的運算結果。
設計調試報告要求:
1.工作原理簡述;
2.設計思路;
3.難點及解決方法;
4.設計、調試結果及分析;
5.程序文本及操作步驟。
標簽:
信號
上傳時間:
2014-01-12
上傳用戶:集美慧
-
算法的許多例子都是最優化問題( optimization problem),每個最優化問題都包含一組限制條件( c o n s t r a i n t)和一個優化函數( optimization function),符合限制條件的問題求解方案稱為可行解( feasible solution),使優化函數取得最佳值的可行解稱為最優解(optimal solution)。
標簽:
optimization
problem
算法
上傳時間:
2014-08-25
上傳用戶:123456wh
-
希爾排序算法:
基本思想:將整個無序序列分割成若干小的子序列分別進行插入排序。 序列分割方法:將相隔某個增量h的元素構成一個子序列。在排序過程中,逐次減小這個增量,最后當h減到1時,進行一次插入排序,排序就完成。增量序列一般采用:ht=2t-1,1≤t≤[log2n],其中n為待排序序列的長度。
標簽:
序列
排序
排序算法
分割
上傳時間:
2013-12-19
上傳用戶:kikye
-
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NULL 0
#define MaxSize 30
typedef struct athletestruct /*運動員*/
{
char name[20];
int score; /*分數*/
int range; /**/
int item; /*項目*/
}ATH;
typedef struct schoolstruct /*學校*/
{
int count; /*編號*/
int serial; /**/
int menscore; /*男選手分數*/
int womenscore; /*女選手分數*/
int totalscore; /*總分*/
ATH athlete[MaxSize]; /**/
struct schoolstruct *next;
}SCH;
int nsc,msp,wsp;
int ntsp;
int i,j;
int overgame;
int serial,range;
int n;
SCH *head,*pfirst,*psecond;
int *phead=NULL,*pafirst=NULL,*pasecond=NULL;
void create();
void input ()
{
char answer;
head = (SCH *)malloc(sizeof(SCH)); /**/
head->next = NULL;
pfirst = head;
answer = 'y';
while ( answer == 'y' )
{
Is_Game_DoMain:
printf("\nGET Top 5 when odd\nGET Top 3 when even");
printf("\n輸入運動項目序號 (x<=%d):",ntsp);
scanf("%d",pafirst);
overgame = *pafirst;
if ( pafirst != phead )
{
for ( pasecond = phead ; pasecond < pafirst ; pasecond ++ )
{
if ( overgame == *pasecond )
{
printf("\n這個項目已經存在請選擇其他的數字\n");
goto Is_Game_DoMain;
}
}
}
pafirst = pafirst + 1;
if ( overgame > ntsp )
{
printf("\n項目不存在");
printf("\n請重新輸入");
goto Is_Game_DoMain;
}
switch ( overgame%2 )
{
case 0: n = 3;break;
case 1: n = 5;break;
}
for ( i = 1 ; i <= n ; i++ )
{
Is_Serial_DoMain:
printf("\n輸入序號 of the NO.%d (0<x<=%d): ",i,nsc);
scanf("%d",&serial);
if ( serial > nsc )
{
printf("\n超過學校數目,請重新輸入");
goto Is_Serial_DoMain;
}
if ( head->next == NULL )
{
create();
}
psecond = head->next ;
while ( psecond != NULL )
{
if ( psecond->serial == serial )
{
pfirst = psecond;
pfirst->count = pfirst->count + 1;
goto Store_Data;
}
else
{
psecond = psecond->next;
}
}
create();
Store_Data:
pfirst->athlete[pfirst->count].item = overgame;
pfirst->athlete[pfirst->count].range = i;
pfirst->serial = serial;
printf("Input name:) : ");
scanf("%s",pfirst->athlete[pfirst->count].name);
}
printf("\n繼續輸入運動項目(y&n)?");
answer = getchar();
printf("\n");
}
}
void calculate() /**/
{
pfirst = head->next;
while ( pfirst->next != NULL )
{
for (i=1;i<=pfirst->count;i++)
{
if ( pfirst->athlete[i].item % 2 == 0 )
{
switch (pfirst->athlete[i].range)
{
case 1:pfirst->athlete[i].score = 5;break;
case 2:pfirst->athlete[i].score = 3;break;
case 3:pfirst->athlete[i].score = 2;break;
}
}
else
{
switch (pfirst->athlete[i].range)
{
case 1:pfirst->athlete[i].score = 7;break;
case 2:pfirst->athlete[i].score = 5;break;
case 3:pfirst->athlete[i].score = 3;break;
case 4:pfirst->athlete[i].score = 2;break;
case 5:pfirst->athlete[i].score = 1;break;
}
}
if ( pfirst->athlete[i].item <=msp )
{
pfirst->menscore = pfirst->menscore + pfirst->athlete[i].score;
}
else
{
pfirst->womenscore = pfirst->womenscore + pfirst->athlete[i].score;
}
}
pfirst->totalscore = pfirst->menscore + pfirst->womenscore;
pfirst = pfirst->next;
}
}
void output()
{
pfirst = head->next;
psecond = head->next;
while ( pfirst->next != NULL )
{
// clrscr();
printf("\n第%d號學校的結果成績:",pfirst->serial);
printf("\n\n項目的數目\t學校的名字\t分數");
for (i=1;i<=ntsp;i++)
{
for (j=1;j<=pfirst->count;j++)
{
if ( pfirst->athlete[j].item == i )
{
printf("\n %d\t\t\t\t\t\t%s\n %d",i,pfirst->athlete[j].name,pfirst->athlete[j].score);break;
}
}
}
printf("\n\n\n\t\t\t\t\t\t按任意建 進入下一頁");
getchar();
pfirst = pfirst->next;
}
// clrscr();
printf("\n運動會結果:\n\n學校編號\t男運動員成績\t女運動員成績\t總分");
pfirst = head->next;
while ( pfirst->next != NULL )
{
printf("\n %d\t\t %d\t\t %d\t\t %d",pfirst->serial,pfirst->menscore,pfirst->womenscore,pfirst->totalscore);
pfirst = pfirst->next;
}
printf("\n\n\n\t\t\t\t\t\t\t按任意建結束");
getchar();
}
void create()
{
pfirst = (struct schoolstruct *)malloc(sizeof(struct schoolstruct));
pfirst->next = head->next ;
head->next = pfirst ;
pfirst->count = 1;
pfirst->menscore = 0;
pfirst->womenscore = 0;
pfirst->totalscore = 0;
}
void Save()
{FILE *fp;
if((fp = fopen("school.dat","wb"))==NULL)
{printf("can't open school.dat\n");
fclose(fp);
return;
}
fwrite(pfirst,sizeof(SCH),10,fp);
fclose(fp);
printf("文件已經成功保存\n");
}
void main()
{
system("cls");
printf("\n\t\t\t 運動會分數統計\n");
printf("輸入學校數目 (x>= 5):");
scanf("%d",&nsc);
printf("輸入男選手的項目(x<=20):");
scanf("%d",&msp);
printf("輸入女選手項目(<=20):");
scanf("%d",&wsp);
ntsp = msp + wsp;
phead = (int *)calloc(ntsp,sizeof(int));
pafirst = phead;
pasecond = phead;
input();
calculate();
output();
Save();
}
標簽:
源代碼
上傳時間:
2016-12-28
上傳用戶:150501